<?php 
	defined('IN_SITE') or exit('Access Denied');
	
	
	switch($action){
		case 'down':
			// 需要计算下载次数的解决方案,方法
			// $downurl = urlencode(site_auth("c=$r[contentid]&f=$r[downurl]", 'ENCODE', AUTH_KEY));
			// <a href="download.php?url=$downurl">
			
			//require dirname(__FILE__).'/includes/common.inc.php';
			$url = site_auth($url, 'DECODE', AUTH_KEY);
			
			// parse_str用来解析（分离）URL中的查询字符串（Query String），所谓查询字符串是指一个URL中?后面的部分，如http://localhost/test/result.php?name=anve&age=21，则查询字符串就是"name=anve&age=21" 当然在PHP里，你可以用$_GET数组来取得查询字符串的值，但有时候parse_str会方便些，特别是当查询字符串中的变量名（对应于上面例子中的name和age）不知道的时候。
			parse_str($url);
			
			//$contentid = intval($c);
			$fileurl = trim($f);
			
			/*include MOD_ROOT.'content/content.class.php';
			$content = new content;
			$data = $content->get($c);
			$content->hits($c);*/
			
			//远程文件
			if(strpos($fileurl, ':/'))
			{
				header("Location: $fileurl");
			}
			//本地文件
			else
			{
				header("Location: ".SITE_URL.$fileurl);
				
			}
		break;
		default:
		
			require_once MOD_ROOT.'attachment/attachment.class.php';
			
			// $module被占用了，这里使用$mod
			$attachment = new attachment($mod);
			if($catid)
			{
				$C = cache_read('category_'.$catid.'.php');
			}
			//dump($C);
			
			$upload_allowext = $C['upload_allowext'] ? $C['upload_allowext'] : UPLOAD_ALLOWEXT;
			$upload_maxsize = $C['upload_maxsize'] ? $C['upload_maxsize'] : UPLOAD_MAXSIZE;
			
			if($dosubmit)
			{
				$attachment->upload('uploadfile', $upload_allowext, $upload_maxsize, 1);
				if($attachment->error) showmessage($attachment->error());
				$imgurl = UPLOAD_URL.$attachment->uploadedfiles[0]['filepath'];	
				$aid = $attachment->uploadedfiles[0]['aid'];
				$filesize = $attachment->uploadedfiles[0]['filesize'];
				$filesize = $attachment->size($filesize);
				
				
				// 开启图片另一服务器同步，实现图片服务器分离 aming
				// 注意：将synchronous(同步).php文件放入另一台服务器下
				if($synchronous = 0){
					
					//另一台服务器地址,以/结束
					$otherhost = "http://demo.68web.net/";
					
					//初始化curl
					$ch = curl_init() or die (curl_error());
					//设置URL参数
					curl_setopt($ch,CURLOPT_URL, $otherhost."synchronous.php?imgurl=".SITE_URL."$imgurl");
					
					//要求CURL返回数据
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
					
					//执行请求, 返回数字类型结果
					$result = curl_exec($ch) or die (curl_error());
					
					// debug
					//echo $result;exit();
					
					//关闭CURL
					curl_close($ch);
					
					$imgurl = $otherhost.$result;
				}
				
				
				if($isthumb || $iswatermark)
				{
					require_once 'image.class.php';
					$image = new image();
					$img = UPLOAD_ROOT.$attachment->uploadedfiles[0]['filepath'];
					if($isthumb)
					{
						$image->thumb($img, $img, $width, $height);
					}
					if($iswatermark)
					{
						$image->watermark($img, $img, $SETTING['watermark_pos'], $SETTING['watermark_img'], '', 5, '#ff0000', $SETTING['watermark_jpgquality']);
					}
				}
				showmessage("文件上传成功！<script language='javascript'>	try{ $(window.opener.document).find(\"form[@name='myform'] #$uploadtext\").val(\"$imgurl\");$(window.opener.document).find(\"form[@name='myform'] #{$uploadtext}_aid\").val(\"$aid\");$(window.opener.document).find(\"form[@name='myform'] #$filesize\").val(\"$filesize\");}catch(e){} window.close();</script>", HTTP_REFERER);
			}
			else
			{
				$upload_maxsize = $attachment->size($upload_maxsize);
				include 'upload.tpl.php';
			}
		break;
	}

?>